class_name ExtensionMethod_ShotBullet
extends ExtensionMethod

@export var muzzle_slot_index:int = 0
@export var prefab_index:int = 0
@export var is_random_get_prefab:bool = false
var weapon:Weapon
var c_slot:WeaponC_Slot
var c_capcity:WeaponC_Capacity
var shot_tween:Tween
var muzzle_slot:Node2D

func init(ex):
	super.init(ex)
	weapon = weaponC_extension_method.weapon
	c_slot = weapon.get_component_by_name("WeaponC_Slot")
	c_capcity = weapon.get_component_by_name("WeaponC_Capacity")
	muzzle_slot = c_slot.get_slot_by_type(GameEnum.SlotType.Weapon,muzzle_slot_index)
func on_equip():
	enable = true
func on_unequip():
	enable = false
	if shot_tween or shot_tween.is_valid():
		shot_tween.kill()
func get_rad() -> float:
	return c_slot.get_slot_by_type(GameEnum.SlotType.Weapon,muzzle_slot_index).global_rotation
func get_prefab() -> PackedScene:
	if is_random_get_prefab:
		return Util.random_get_count(weapon.weapon_data.prefabs,1,true)
	else:
		return weapon.weapon_data.prefabs[prefab_index]
func recount(arr:Array,new_count) -> Array:
	if new_count == 1:
		return [0]
	else:
		var new_arr:Array = []
		var l = arr[-1] - arr[0]
		for i in range(new_count):
			new_arr.append(arr[0] + i * l/(new_count - 1))
		return new_arr
func create_prefab(count:int,angles:Array):
	if count == 1:
		if not c_capcity.has_capacity(count):
			shot_tween.kill()
			c_capcity.get_from_capacity(0)
			return
		else:
			c_capcity.get_from_capacity(count)
			var prefab = get_prefab().instantiate()
			prefab.global_position = muzzle_slot.global_position
			prefab.rotate(muzzle_slot.global_rotation + deg_to_rad(angles[0]))
			var owner_entity_camp_component = weaponC_extension_method.weapon.owner_entity.get_component_by_name("CampComponent") as CampComponent
			var data = prefab.get_entity_data()
			data.source_entity = weaponC_extension_method.weapon.owner_entity
			data.default_camp = owner_entity_camp_component.cur_camp
			GameEffectSystem.add_child(prefab)
	elif count > 0:
		var real_count = c_capcity.get_from_capacity(count)
		var real_angles:Array = []
		if real_count != count:
			real_angles = recount(angles,real_count)
		else:
			real_angles = angles
		if real_count == 0:
			c_capcity.get_from_capacity(0)
		else:
			for i in range(real_count):
				var prefab = get_prefab().instantiate()
				prefab.global_position = muzzle_slot.global_position
				prefab.rotate(muzzle_slot.global_rotation + deg_to_rad(real_angles[i]))
				GameEffectSystem.add_child(prefab)
func shot_bullet():
	if shot_tween and shot_tween.is_valid():
		shot_tween.kill()
	shot_tween = create_tween()
	for i in weapon.weapon_data.get_round_amount():
		var count = weapon.weapon_data.get_count_per_round()
		if weapon.weapon_data.interval_per_one > 0:
			var angles:Array = []
			if count == 1:
				shot_tween.tween_callback(create_prefab.bind(1,[0]))
			else:
				var angle:float = weapon.weapon_data.get_scattering_angle()
				for idx in range(count):
					shot_tween.tween_callback(create_prefab.bind(1,[-angle/2 + idx * angle/(count-1)]))
					if idx < count - 1: 
						shot_tween.tween_interval(weapon.weapon_data.interval_per_one)
		else:
			var angles:Array = []
			if count == 1:
				angles.append(0)
			else:
				var angle:float = weapon.weapon_data.get_scattering_angle()
				for idx in range(count):
					angles.append(-angle/2 + idx * angle/(count-1))
			shot_tween.tween_callback(create_prefab.bind(count,angles))
		if i < weapon.weapon_data.get_round_amount() - 1 and weapon.weapon_data.interval_per_round > 0:
			shot_tween.tween_interval(weapon.weapon_data.interval_per_round)
